import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/assets/assets.dart';
import 'package:pornhub_app/components/activity_cell/activity_cell.dart';
import 'package:pornhub_app/components/image_view.dart';
import 'package:pornhub_app/components/app_bar/app_bar_view.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_widget.dart';
import 'package:pornhub_app/components/no_more/no_data_scroll_view.dart';
import 'package:pornhub_app/components/text_view.dart';
import 'package:pornhub_app/generate/app_image_path.dart';
import 'package:pornhub_app/model/mine/fans_follower_model.dart';
import 'package:pornhub_app/model/mine/official_community_model.dart';
import 'package:pornhub_app/routes/routes.dart';
import 'package:pornhub_app/utils/ad_jump.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/utils/extension.dart';
import '../controllers/fans_followers_page_controller.dart';

class FansFollowersPage extends StatefulWidget {
  const FansFollowersPage({super.key});

  @override
  _FansFollowersPageState createState() => _FansFollowersPageState();
}

class _FansFollowersPageState extends State<FansFollowersPage> {
  final controller = Get.find<FansFollowersPageController>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: COLOR.color_161616,
      appBar: AppBarView(
        title: Obx(() => TextView(
              text: controller.title.value,
              color: Colors.white,
            )),
      ),
      body: _bodyViews(),
    );
  }

  Widget _bodyViews() {
    return BaseRefreshWidget(
      controller,
      refreshOnStart: true,
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 14.w),
        child: Obx(
          () {
            bool isHaveData = false;
            if (controller.title.value == "官方粉丝群") {
              isHaveData = controller.ocLists.isNotEmpty;
            } else {
              isHaveData = controller.list.isNotEmpty;
            }
            return isHaveData
                ? ListView.builder(
                    itemCount: controller.title.value == "官方粉丝群"
                        ? controller.ocLists.value.length
                        : controller.list.value.length,
                    itemBuilder: (context, index) {
                      return controller.title.value == "官方粉丝群"
                          ? _itemOCView(controller.ocLists.value[index])
                          : _itemFansFollowerView(
                              controller.list.value[index], index);
                    },
                  )
                : const NoDataScrollView();
          },
        ),
      ),
    );
  }

  Widget _itemFansFollowerView(FansFollowerModel model, int index) {
    return Container(
      height: 44.h,
      width: double.infinity,
      margin: EdgeInsets.only(bottom: 15.h),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ImageView(
            src: model.logo ?? "",
            width: 44.w,
            height: 44.w,
            fit: BoxFit.cover,
            defaultPlace: Assets.avatarplaceholer,
            borderRadius: BorderRadius.circular(20.w),
          ).onTap(() {
            // Get.toBloggerDetail(userId: model.userId ?? 0);
          }),
          Expanded(
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 10.w),
              child: TextView(
                text: "${model.nickName}",
                fontSize: 14.sp,
                color: COLOR.white,
                fontWeight: FontWeight.w600,
              ),
            ),
          ),
          _followButton(model, index),
        ],
      ),
    );
  }

  Widget _itemOCView(OfficialCommunityModel model) {
    return Container(
      height: 78.w,
      padding: EdgeInsets.only(left: 10.w, right: 10.w),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10.w),
          color: COLOR.white.withValues(alpha: 0.1)),
      width: double.infinity,
      margin: EdgeInsets.only(bottom: 10.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ImageView(
            src: model.groupType == 2
                ? AppImagePath.mine_potato
                : AppImagePath.mine_telegram,
            width: 44.w,
            height: 44.w,
            fit: BoxFit.cover,
          ),
          Expanded(
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 10.w),
              child: TextView(
                text: "${model.name}",
                fontSize: 15.w,
                color: COLOR.white,
                fontWeight: FontWeight.w600,
              ),
            ),
          ),
          GestureDetector(
            onTap: () async {
              SmartDialog.showLoading(msg: '操作中...');
              jumpExternalAddress(model.link ?? "", null);
              SmartDialog.dismiss();
            },
            child: Container(
              width: 70.w,
              height: 32.w,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: COLOR.color_FF9205,
                borderRadius: BorderRadius.circular(16.w),
              ),
              child: TextView(
                text: "加群",
                fontSize: 13.w,
                color: COLOR.black,
                fontWeight: FontWeight.w600,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _followButton(FansFollowerModel model, int index) {
    final isAttention = model.attention ?? false;
    return GestureDetector(
      onTap: () async {
        SmartDialog.showLoading(msg: '操作中...');
        if (isAttention) {
          await controller.follwersCancel(toUserId: model.userId ?? 0);
        } else {
          await controller.follwers(toUserId: model.userId ?? 0);
        }
        controller.list.value[index].attention = !isAttention;
        SmartDialog.dismiss();
        setState(() {});
      },
      child: Container(
        width: 70.w,
        height: 25.h,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          border: isAttention
              ? Border.all(color: COLOR.color_A4A4B2, width: 1)
              : Border.all(color: COLOR.color_B940FF, width: 1),
          borderRadius: BorderRadius.circular(19.w),
        ),
        child: TextView(
          text: isAttention ? "已关注" : "关注",
          fontSize: 12.sp,
          color: isAttention ? COLOR.color_A4A4B2 : COLOR.color_B940FF,
        ),
      ),
    );
  }
}
